package com.sun.ftl;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Suncl
 * @Date: 2023/3/28 19:43
 */
public class ftlTest {
    public static void main(String[] args) {
        String str = "Hello_World";
        System.out.println(StrUtil.toCamelCase(str));
        System.out.println(StrUtil.toUnderlineCase(str));
    }

    private static final String path = "D:\\codebase\\javaLearn\\SpringMVC\\src\\main\\resources\\ftl";


    @Test
    public void test01() throws IOException, TemplateException {

        Configuration configuration = getConfiguration(path);

        Template template = configuration.getTemplate("Demo.ftl");

        Map map = Maps.newHashMap();

        map.put("serviceName", "PersonService");
        map.put("method", "personMethod1");

        FileWriter writer = new FileWriter(new File(path + File.separator + "Demo1.java"));


        template.process(map, writer);


    }

    @Test
    public void test2() throws IOException, TemplateException {

        Configuration configuration = getConfiguration(path);
        Template template = configuration.getTemplate("Bean.ftl");
        Map map = Maps.newHashMap();
        List<String> fields = Arrays.asList("id", "name", "age", "addr", "sex");
        List list = Lists.newArrayList();
        for (int i = 0; i < 5; i++) {
            Map bean = Maps.newHashMap();
            bean.put("type", "String");
            bean.put("field", fields.get(i));
            list.add(bean);
        }
        String tableName = "PERSON_INFO";
        map.put("tableName", StrUtil.toCamelCase(tableName));
        map.put("list", list);

        FileWriter writer = new FileWriter(new File(path + File.separator + map.get("tableName") + ".java"));


        template.process(map, writer);
    }


    /**
     * TABLE_CAT String => 表类别
     * TABLE_SCHEM String => 表模式
     * TABLE_NAME String => 表名称
     * TABLE_TYPE String => 表类型
     * REMARKS String => 表备注
     *
     *
     *
     */
    @Test
    public void test3() {
        Connection connection = null;
        List tableDefins = Lists.newArrayList();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT", "root", "root");

            tableDefins = getTableDefins(connection);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }


    public List getTableDefins(Connection connection) throws Exception {

        DatabaseMetaData metaData = connection.getMetaData();

        ResultSet tables = metaData.getTables(null, "test", "qrtz%", new String[]{"TABLE"});

        List tableDefins = Lists.newArrayList();
        while (tables.next()) {
            System.out.println(tables.getString("TABLE_NAME"));
            System.out.println(tables.getString("REMARKS"));

            TableDefine tableDefine = new TableDefine();
            tableDefine.setTableName(tables.getString("TABLE_NAME"));
            tableDefine.setNote(tables.getString("REMARKS"));

            List fileds = Lists.newArrayList();
            ResultSet dataColumns = metaData.getColumns(null, metaData.getUserName(), tables.getString("TABLE_NAME"), null);
            while (dataColumns.next()) {
                String columnName = dataColumns.getString("COLUMN_NAME");
                String remarks = dataColumns.getString("REMARKS");
                String typeName = dataColumns.getString("TYPE_NAME");
                fileds.add(new FieldDefine(columnName, remarks, typeName));
            }
            tableDefine.setFields(fileds);
            tableDefins.add(tableDefine);
        }
        return tableDefins;
    }

    public Configuration getConfiguration(String path) {
        Configuration configuration = new Configuration(Configuration.getVersion());
        try {
            configuration.setDirectoryForTemplateLoading(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        configuration.setDefaultEncoding("utf-8");
        return configuration;
    }


}

@Data
class TableDefine {
    //表命
    private String tableName;
    //备注
    private String note;
    //表字段
    private List fields;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class FieldDefine {
    //字段
    private String field;

    //字段类型
    private String type;

    //备注
    private String note;


}